<template>
  <div class="addBusiness">
    <el-dialog :visible.sync="dialogFormVisible" width="600px" :center="true">
      <div slot="title" class="title">{{mode=="add"?'新增企业':'编辑企业'}}</div>

      <el-form :model="form" label-width="110px" :rules="rules" ref="form">
        <el-form-item label="企业编号" prop="eid">
          <el-input v-model="form.eid"></el-input>
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="企业简称" prop="short_name">
          <el-input v-model="form.short_name"></el-input>
        </el-form-item>
        <el-form-item label="企业简介" prop="intro">
          <el-input v-model="form.intro"></el-input>
        </el-form-item>
        <el-form-item label="企业备注" prop="remark">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addBusiness, editBusiness } from "@/api/business.js";
export default {
  props: ["mode"],
  data() {
    return {
      dialogFormVisible: false,
      form: {
        eid: "",
        name: "",
        short_name: "",
        intro: "",
        remark: ""
      },
      rules: {
        eid: [{ required: true, message: "请填写企业编号", trigger: "change" }],
        name: [
          { required: true, message: "请填写企业名称", trigger: "change" }
        ],
        short_name: [
          { required: true, message: "请填写企业简称", trigger: "change" }
        ],
        intro: [
          { required: true, message: "请填写企业简介", trigger: "change" }
        ]
      }
    };
  },

  watch: {
    dialogFormVisible(newVal) {
      if (newVal == false) {
        // 清除表单及验证
        this.$refs.form.resetFields();
      }
    }
  },

  methods: {
    submit() {
      // 全局验证
      this.$refs.form.validate(result => {
        if (result) {
          // 新增
          if (this.mode == "add") {
            addBusiness(this.form).then(res => {
              console.log("企业新增", res);
              this.$message.success("企业新增成功");
              // 关闭对话框
              this.dialogFormVisible = false;
              // 刷新数据
              this.$emit("input");
            });
          }else {
            // 编辑
            editBusiness(this.form).then(res => {
              console.log('企业编辑', res);
              this.$message.success('编辑成功')
              this.dialogFormVisible = false
              this.$emit('input')
            })
          }
        }
      });
    }
  }
};
</script>

<style lang="less">
.addBusiness {
  .el-dialog__header {
    padding: 0;
  }

  .title {
    height: 53px;
    line-height: 53px;
    background: linear-gradient(to right, #01c5fa, #1394fa);
    color: #fff;
    font-size: 18px;
    font-weight: bold;
  }

  .el-dialog__headerbtn .el-dialog__close {
    color: #fff;
    font-size: 12px;
  }
}
</style>